package com.qingxin.park.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.reflect.TypeToken;
import com.qingxin.park.entity.BatchProductsView;
import com.qingxin.park.entity.DetectionData;
import com.qingxin.park.service.BatchProductsService;
import com.qingxin.park.service.DetectionDataService;
import com.qingxin.park.vo.CropType;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.SysUser;
import com.qingxin.sys.utils.JsonUtil;
import com.qingxin.sys.utils.StringUtils;

/**
 * 农残检测仪
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/fdfcp/external")
public class NcjcyAction extends BaseAction {
	
	@Autowired
	private DetectionDataService detectionDataService;
	
	@Autowired
	private BatchProductsService batchProductsService;
	
	
	
	@RequestMapping(value="/login.action", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object login(String loginName, String password, HttpServletRequest request){
		Map<String, Object> map = new HashMap<>();
		
		if(StringUtils.isEmpty(loginName) || StringUtils.isEmpty(password)){
			map.put("message", "登录失败！");
			map.put("success", false);
			return map;
		}
		SysUser sysUser = sysUserService.getByProperty("login_name", loginName);
		if(sysUser == null){
			map.put("message", "登录失败！");
			map.put("success", false);
			return map;
		}
		
		password = StringUtils.md5(password+sysUser.getSalt());
		if(!sysUser.getPassword().equalsIgnoreCase(password)){
			map.put("message", "登录失败！");
			map.put("success", false);
			return map;
		}
		
		map.put("message", "登录成功！");
		map.put("success", true);
		
		Map<String, Object> data = new HashMap<>();
		data.put("unid", sysUser.getUserid());
		data.put("name", sysUser.getUsername());
		map.put("data", data);
		
		return map;
	}
	
	
	@RequestMapping(value="/saveDetectionHistoryData.action", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object saveDetectionHistoryData(String data, HttpServletRequest request){
		Map<String, Object> rsmap = new HashMap<>();
		
		System.out.println(data);
		
		List<Map<String, String>> list = JsonUtil.toList(data, new TypeToken<List<Map<String, String>>>(){}.getType());
		for(Map<String, String> map : list){
			DetectionData detectionData = new DetectionData();
			detectionData.setDetectionItem(map.get("detectionItem"));
			detectionData.setSampleName(map.get("sampleName"));
			detectionData.setDetectionResult(map.get("detectionResult"));
			detectionData.setEnterpriseUnid(map.get("enterpriseUnid"));
			detectionData.setEnterpriseName(map.get("enterpriseName"));
			detectionData.setDetectionDate(map.get("detectionDate"));
			detectionData.setStandardValue(map.get("standardValue"));
			detectionData.setInhibitionRate(map.get("inhibitionRate"));
			detectionData.setConcentration(map.get("concentration"));
			detectionData.setDetectionUnitName(map.get("detectionUnitName"));
			detectionData.setPersonUuid(map.get("personUuid"));
			detectionData.setPersonName(map.get("personName"));
			detectionData.setDeviceSerialNumber(map.get("deviceSerialNumber"));
			
			detectionData.setTestStandard("");
			detectionData.setTestStatus("0");
			detectionData.setTestResult("");
			
			String productCode = map.get("productCode");
			detectionData.setProductCode(productCode);
			
			BatchProductsView products = batchProductsService.getByPdtcode(productCode);
			if(products == null){
				rsmap.put("message", "上传失败，产品编号无效！");
				rsmap.put("success", false);
				rsmap.put("data", "");
				return rsmap;
			}
			
			detectionData.setCompanyid(products.getCompanyid());
			detectionData.setCompanyName(products.getCompanyName());
			detectionData.setParkid(products.getParkid());
			detectionData.setParkName(products.getParkName());
			detectionData.setPdtid(products.getPdtid());
			detectionData.setPdtname(products.getPdtname());
			detectionData.setPdttype(products.getPdttype());
			
			detectionData.setCropid(products.getCropid());
			detectionData.setCropName(products.getCropName());
			
			CropType cropType = detectionDataService.getCropType(products.getPdttype(), products.getCropid());
			if(cropType!=null){
				detectionData.setTypeid(cropType.getId());
				detectionData.setTypename(cropType.getName());
			}else{
				detectionData.setTypeid("");
				detectionData.setTypename("");
			}
			
			detectionDataService.add(detectionData, request);
			batchProductsService.updateNcsc(products.getPdtid());
		}
		
		rsmap.put("message", "上传成功！");
		rsmap.put("success", true);
		rsmap.put("data", "");
		return rsmap;
	}
	
	
	
}
